<?php

namespace backend\controllers;


use backend\component\BackendBaseController;
use backend\component\ResultFormat;
use backend\service\TagService;
use common\component\Pinyin;
use common\utils\CommonUtil;
use common\models\info\Tags;
use common\models\info\TagsRelation;
use frontend\service\TravelService;


class TagController extends BackendBaseController
{
    public function actions()
    {
      return [
          "modify" => [
              'class' => "backend\actions\ModifyAction",
              'modelName'=> Tags::className(),
          ],
          "delete" =>[
            'class' => "backend\actions\DeleteAction",
            'modelName'=> Tags::className(),
            'is_physics' => true
         ],
      ];
    }
    /*
     * 标签列表
     */
    public function actionList() {
        $data = TagService::tagsList(\Yii::$app->request->get());
        return ResultFormat::success('SUCCESS',json_decode($data));
    }

    public function actionEdit() {
        $id = \Yii::$app->request->get("id",0);
        $model = Tags::modelById($id);
        return ResultFormat::success('SUCCESS',$model->attributes);
    }
    /*
     * 关联的文章
     */
    public function actionArtotal() {
        if (\Yii::$app->request->getIsAjax()) {
            $_POST['tag_id'] = \Yii::$app->request->get("tag_id");
            return TagService::articleRelationList(\Yii::$app->request->post());
        }
        $model = new TagsRelation();
        $tag_id = \Yii::$app->request->get("tag_id");
        return $this->render("artlist",['model'=>$model,'tag_id'=>$tag_id]);
    }

    /**
     * 解除话题对应的文章
     */
    public function actionArtrelieve() {
        $object_id = \Yii::$app->request->get("object_id");
        $tag_id    = \Yii::$app->request->get("tag_id");
        $type = TagsRelation::TYPE_ARTICLE;
        $result = TagService::artRelieve($object_id,$tag_id,$type);
        return $result;
    }
}